var SELL_PATH_PREFIX="";

function setSellChannelPath(path){
	SELL_PATH_PREFIX = path;
}

$(function(){
	WASP_WIDGET.triggerInstSelect("#instId");
	WASP_WIDGET.triggerDateStyleWithYMD("#expRaiseBegDate");
	WASP_WIDGET.triggerDateStyleWithYMD("#expRaiseEndDate");
	WASP_WIDGET.triggerIRadio("input[type='radio'][name='isEContract']");
	setSellChannelPath(prdSellChannelPath);
	initValidate();
	bindRadioClick();
	bindSelectClick();
});
 
function bindSelectClick(){
	$("#instId").on("change", function(){
		$(this).valid && $(this).valid();
	}); 
}

function bindRadioClick(){
	$("input[type='radio'][name='isEContract']").on('ifChecked', function() {
		var isEContract = $(this).val();
		if(isEContract=='1'){
			$("#versionNumTxt").html("*");
			$("#versionNum").rules("add",{
				required : true,messages:{required:"请选择版本号"}
			});
		}else{
			$("#versionNumTxt").html("");
			$("#versionNum").rules('remove','required');
		}
		
	});
}

//表单验证
function initValidate(){
	 $("#prdSellChannelForm").validate({
	        focusCleanup: true,
	        rules: {
	        	expRaiseBegDate:{
	            	required: true	
	            },
	            expRaiseEndDate:{
	            	required: true
	            },	
	            isEContract : {
	            	required: true
				},
				instId : {
					required: true
				},
	        },
	        messages: {
	        	expRaiseBegDate : "请选择募集起始日",
	        	expRaiseEndDate : "请选择募集结束日",
	        	isEContract : "请选择是否采用电子合同",
	        	instId : "请选择销售机构",
	        }
	    });
}

function closeLayer(){
	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
	parent.layer.close(index); //再执行关闭
}

function save(){
	if(!$("form[name='prdSellChannelForm']").valid()) {
		toastr.warning('', '请完善信息');
		return false;
	}
	var starttime = $('#expRaiseBegDate').val();
	var endtime = $('#expRaiseEndDate').val();
	if(moment(starttime, "YYYY/MM/DD")>moment(endtime, "YYYY/MM/DD")){
		toastr.warning('', '募集结起始日不能大于募集结束日');
		return false;
	}
	$('#prdSellChannelForm').attr('action', SELL_PATH_PREFIX + "/addPrdSellChannel.do");
	//提交表单
	SubmitAndPreventSecond("prdSellChannelForm",true);
}